package barcode.android.dao.impl;

import junit.framework.Assert;
import barcode.android.activity.Barcode_androidActivity;
import barcode.android.dao.LocationsDao;
import barcode.android.dao.impl.BaseDaoImpl;
import barcode.android.dao.impl.SqliteDatabaseState;
import barcode.android.model.Locations;
import android.test.ActivityInstrumentationTestCase2;

public class LocationsDaoTest extends
		ActivityInstrumentationTestCase2<Barcode_androidActivity> {

	public LocationsDaoTest() {
		super(Barcode_androidActivity.class);
	}

	public void setUp() {
		BaseDaoImpl.initContext(getActivity().getBaseContext());
	}

	public void testAddLocations() {
		Locations loc = new Locations();
		loc.setName("韵苑操场");
		loc.setX(12);
		loc.setY(83);

		LocationsDao dao = new LocationsDaoImpl();
		boolean res = false;
		try {
			dao.beginTransaction(SqliteDatabaseState.Writable);
			res = dao.add(loc);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertEquals(true, res);
	}

	public void testQueryLocations() {
		LocationsDao dao = new LocationsDaoImpl();
		Locations loc = null;
		try {
			dao.beginTransaction(SqliteDatabaseState.Readable);
			loc = dao.query(1);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertNotNull(loc);
		Assert.assertEquals("韵苑操场", loc.getName());
	}

	public void testDeleteLocations() {
		LocationsDao dao = new LocationsDaoImpl();
		boolean res = false;
		try {
			dao.beginTransaction(SqliteDatabaseState.Writable);
			res = dao.delete(1);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertEquals(true, res);
	}

	public void testUpdateLocations() {
		Locations loc = new Locations();
		loc.setId(1);
		loc.setName("中操场");
		loc.setX(1322);
		loc.setY(9.233);

		LocationsDao dao = new LocationsDaoImpl();
		boolean res = false;
		try {
			dao.beginTransaction(SqliteDatabaseState.Writable);
			res = dao.update(loc);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertEquals(true, res);

		try {
			dao.beginTransaction(SqliteDatabaseState.Readable);
			loc = dao.query(1);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertEquals("中操场", loc.getName());
		Assert.assertEquals(1322.0, loc.getX());
		Assert.assertEquals(9.233, loc.getY());
	}
}
